/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package spacefiller;

import java.io.Console;
import java.io.IOException;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author iktsup
 */
public class Main {
    public static final String version  = "v0.0.3a";
    public static final String MB       = "MB";
    public static final String GB       = "GB";
    public static final String TB       = "TB";
    public static final String PB       = "PB";


    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        java.io.FileWriter out = null;
        try {
            Console console = System.console();
            String sSize = "1";
            double val = 0.0;
            String data = "something";

            System.out.println("##-- SARCO SPACE FILLER "
                    + version + " --##");
            System.out.println("Examples: ");
            System.out.println("100 ((Default is MB) File output will be 100MB");
            System.out.println("100GB (File output will be 100GB");
            System.out.println("100TB (File output will be 100TB");
            System.out.println("100PB (File output will be 100PB");
            
            if (console != null) {
                sSize = console.readLine("Output Size (MB): ");
                if (sSize.endsWith(MB)) {
                    val = Math.pow(1024, 2) / data.length();
                    sSize = sSize.replaceAll(MB, "");
                }
                else if (sSize.endsWith(GB)) {
                    val = Math.pow(1024, 3) / data.length();
                    sSize = sSize.replaceAll(GB, "");
                }
                else if (sSize.endsWith(TB)) {
                    val = Math.pow(1024, 4) / data.length();
                    sSize = sSize.replaceAll(TB, "");
                }
                else if (sSize.endsWith(PB)) {
                    val = Math.pow(1024, 5) / data.length();
                    sSize = sSize.replaceAll(PB, "");
                } else {
                    val = Math.pow(1024, 2) / data.length();
                }
            } else {
                val = Math.pow(1024, 2) / data.length();
            }
            
            val *= Double.parseDouble(sSize);
            int size = (int) val;
            out = new java.io.FileWriter("data.dat");

            long lastPercent = -1;
            long start = Calendar.getInstance().getTime().getTime() / 1000;
            long current = Calendar.getInstance().getTime().getTime() / 1000;
            String eta = "";

            System.out.println("Starting!");
            for (int i = 0; i < size; i++) {

                // Percent calculation
                double dPercent = 0;
                long iPercent = 0;
                long c = current - start;
                
                if (i > 0) {
                    dPercent = (double)i / size * 100;
                    iPercent = (long) Math.round(dPercent);
                }     
                out.write(data);
                if (iPercent != lastPercent) {
                    if (current > -1) {
                        long p = iPercent;
                        long e = 0;
                        
                        if (c > 0 && p > 0) {
                            e = (c / p) * 100;
                            long x = e - c;
                            current = x;
                        } else {
                            current = e;
                        }
                        // Hour
                        if (current > 3600) {
                            
                            eta = (current / 3600) + "hour left!";
                        }
                        // Min
                        else if(current > 60) {
                            eta = (current / 60) + "mins left!";
                        }
                        // Sec
                        else {
                            eta = current + "secs left!";
                        }
                    }
                    System.out.println(iPercent + "% Done!");
                    System.out.println(eta);
                }
                lastPercent = iPercent;
                current = Calendar.getInstance().getTime().getTime() / 1000;
            }
            System.out.println("Ending!");
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                out.close();
            } catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
